<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<div>
			<label for="total">猴子的总数:</label>
			<input type="text" name="total" id="total" value="" />
			<br />
			<label for="kick">踢出猴子编号:</label>
			<input type="text" name="kick" id="kick" value=""/>
			<br />
			<label for="king">大王:</label>
			<input type="text" name="king" id="king" value="" disabled=""/>
			<br />
			<input type="button" value="开始选大王" id="btn"/>
		</div>
	</body>
	<script>
		var btn = document.getElementById("btn");
		btn.onclick = function(){
			var total = document.getElementById("total").value;
		var kick = document.getElementById("kick").value;
		var monkey=[];
		//保存猴子的编号，monkey[0]=1
		for(var i=1;i<=total;i++){
			monkey.push(i);
		}
		i=1;
		while(monkey.length>1){
			var head = monkey.shift();//移出最前面的编号
			if(i%kick != 0){
				monkey.push(head);
			}
			i++;
		}
		
		document.getElementById("king").value=monkey[0];
			
		}
		
	</script>
</html>
